#!/usr/bin/env python

# -------
# Imports:
# -------
import os, re
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from settings import *
from core.models import *
import logging

# -----------------------------------
class GetImage(webapp.RequestHandler):
# -----------------------------------
	# -----------
    def get(self):
	# -----------
        # Parse out the URL's ID
        urlIdM = re.search(r'/image/(?P<type>\w+)/(?P<id>\w+)', self.request.path)
        urlId = int(urlIdM.group('id'))
        urlType = urlIdM.group('type')

        # Query for the image
        image = Image.get_by_id(urlId) if urlType == 'source' else ThresholdedImage.get_by_id(urlId)

        # Make sure we found something
        if image is not None:
            # Return the image
            self.response.headers['Content-Type'] = 'image/PNG'
            self.response.out.write(str(image.image))
            logging.debug('Done!')
